﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Labthink.PCSoft.UniversalHost4.DataAccess;
using Labthink.PCSoft.UniversalHost4.Instrument.Model;
using Labthink.PCSoft.UniversalHost4.Model;
using Labthink.PCSoft.UniversalHost4.Utility;
using NLog;

namespace Labthink.PCSoft.UniversalHost4
{
    internal static class SerialPortHelper
    {
        public static readonly byte[] WriteBuf = new byte[7];
        private static readonly Logger LogCom = LogManager.GetLogger("ComData");
        private static readonly Logger Log = LogManager.GetCurrentClassLogger();
        private static int _colWidth = 4;
        private static readonly List<byte> Buf = new List<byte>();

        private static EventHandler<ComParameterEventArgs> _onReceiveParameterHandler;
        public static event EventHandler<ComParameterEventArgs> OnReceiveParameterHandler
        {
            add
            {
                if (_onReceiveParameterHandler == null || !_onReceiveParameterHandler.GetInvocationList().Contains(value))
                {
                    _onReceiveParameterHandler += value;
                }
            }
            remove => _onReceiveParameterHandler -= value;
        }

        private static EventHandler<ComRealTimeDataEventArgs> _onReceiveRealtimeDataHandler;
        public static event EventHandler<ComRealTimeDataEventArgs> OnReceiveRealtimeDataHandler
        {
            add
            {
                if (_onReceiveRealtimeDataHandler == null || !_onReceiveRealtimeDataHandler.GetInvocationList().Contains(value))
                {
                    _onReceiveRealtimeDataHandler += value;
                }
            }
            remove => _onReceiveRealtimeDataHandler -= value;
        }

        private static EventHandler<ComResultEventArgs> _onReceiveResultHandler;
        public static event EventHandler<ComResultEventArgs> OnReceiveResultHandler
        {
            add
            {
                if (_onReceiveResultHandler == null || !_onReceiveResultHandler.GetInvocationList().Contains(value))
                {
                    _onReceiveResultHandler += value;
                }
            }
            remove => _onReceiveResultHandler -= value;
        }

        private static EventHandler<ComStartEventArgs> _onTestStartedHandler;
        public static event EventHandler<ComStartEventArgs> OnTestStartedHandler
        {
            add
            {
                if (_onTestStartedHandler == null || !_onTestStartedHandler.GetInvocationList().Contains(value))
                {
                    _onTestStartedHandler += value;
                }
            }
            remove => _onTestStartedHandler -= value;
        }

        private static EventHandler<EventArgs> _onTestStopHandler;
        public static event EventHandler<EventArgs> OnTestStopHandler
        {
            add
            {
                if (_onTestStopHandler == null || !_onTestStopHandler.GetInvocationList().Contains(value))
                {
                    _onTestStopHandler += value;
                }
            }
            remove => _onTestStopHandler -= value;
        }

        private static EventHandler<ComTestStatusEventArgs> _onTestStatusChangedHandler;
        public static event EventHandler<ComTestStatusEventArgs> OnTestStatusChangedHandler
        {
            add
            {
                if (_onTestStatusChangedHandler == null || !_onTestStatusChangedHandler.GetInvocationList().Contains(value))
                {
                    _onTestStatusChangedHandler += value;
                }
            }
            remove => _onTestStatusChangedHandler -= value;
        }

        private static EventHandler<ComByteCountEventArgs> _onReceivedByteCountChangedHandler;
        public static event EventHandler<ComByteCountEventArgs> OnReceivedByteCountChangedHandler
        {
            add
            {
                if (_onReceivedByteCountChangedHandler == null || !_onReceivedByteCountChangedHandler.GetInvocationList().Contains(value))
                {
                    _onReceivedByteCountChangedHandler += value;
                }
            }
            remove => _onReceivedByteCountChangedHandler -= value;
        }

        private static EventHandler<ComByteCountEventArgs> _onSentByteCountChangedHandler;
        public static event EventHandler<ComByteCountEventArgs> OnSentByteCountChangedHandler
        {
            add
            {
                if (_onSentByteCountChangedHandler == null || !_onSentByteCountChangedHandler.GetInvocationList().Contains(value))
                {
                    _onSentByteCountChangedHandler += value;
                }
            }
            remove => _onSentByteCountChangedHandler -= value;
        }

        public static int ReceiveByteCount { get; set; }

        public static int SentByteCount { get; set; }

        public const int Function = 2;
        public const int Sum = 27;

        /// <summary>
        /// 打开设置的串口
        /// </summary>
        public static bool OpenSerialPort(string port = null)
        {
            string baud = Common.ComInfo.BaudRate + "";
            string dataBits = Common.ComInfo.DataBits + "";
            string par = Common.ComInfo.Parity;
            string stopBits = Common.ComInfo.StopBits + "";

            if (States.Instance.SerialPort != null && States.Instance.SerialPort.IsOpen)
            {
                States.Instance.SerialPort.ClosePort();
            }
            if (port == null)
            {
                // 获取串口号
                port = Common.ComInfo.PortName;
            }
            var sp = new SerialPortExtend(port, baud, dataBits, par, stopBits);
            if (SerialPortExtend.Exists(sp.PortName))
            {
                sp.OpenPort();
                if (sp.IsOpen)
                {
                    States.Instance.SerialPort = sp;
                    States.Instance.SerialPort.DataReceived += SerialPort_DataReceived;
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 发送命令
        /// </summary>
        public static void SendCommand(byte x1, byte x2, byte au)
        {
            WriteBuf[0] = 0xAA;
            WriteBuf[1] = 0x5;
            WriteBuf[2] = 0x3;
            WriteBuf[3] = x1;
            WriteBuf[4] = x2;
            WriteBuf[5] = au;
            WriteBuf[6] = (byte)(x1 + x2 + au);
            WriteData();

            string str = GetComString(WriteBuf);
            Log.Info("发送命令: " + str);
        }

        private static string GetComString(IEnumerable<byte> data)
        {
            string str = "";
            foreach (byte b in data)
            {
                str += b.ToString("X2");
            }

            return str;
        }

        private static void WriteData()
        {
            if (States.Instance.SerialPort != null)
            {
                States.Instance.SerialPort.WriteData(WriteBuf);
                SentByteCount += WriteBuf.Length;
                OnSentByteCountChanged(SentByteCount);
            }
        }

        private static void CalculateSum()
        {
            byte sum = 0;

            for (int i = 3; i < Sum; i++)
            {
                sum += WriteBuf[i];
            }

            WriteBuf[Sum] = sum;
        }


        public static void SerialPort_DataReceived(DataReceivedEventArgs e)
        {
            if (Common.ComInfo.SaveComData)
            {
                LogCom.Info(GetComString(e.Data));
            }

            int nHandled = -1;
            Buf.AddRange(e.Data);
            ReceiveByteCount += e.Data.Count();
            OnReceivedByteCountChanged(ReceiveByteCount);
            //一帧数据长度不定
            int nDataCount = 0;
            for (int nPos = 0; nPos < Buf.Count - 5; nPos++) //一帧最少6个字节
            {
                byte sum = 0;
                if (Buf[nPos + 0] == 0xAA && Buf[nPos + 1] > 0 && Buf[nPos + 1] < 0x6)
                {
                    nDataCount = Buf[nPos + 2]; //数据长度字节
                    for (int i = 3; i < nDataCount + 3; i++)
                    {
                        sum += Buf[nPos + i];
                    }

                    if (sum == Buf[nPos + nDataCount + 3])
                    {
                        List<byte> frame = Buf.GetRange(nPos, nDataCount + 3);
                        try
                        {
                            DataHandle(frame);
                        }
                        catch (Exception ex)
                        {
                            string comStr = "";
                            foreach (byte b in e.Data)
                            {
                                comStr += b.ToString("X2");
                            }

                            Log.Info("COM Error:" + comStr);
                            Log.Error(ex.Message);
                            Log.Error(ex.StackTrace);
                        }
                        nHandled = nPos;
                        nPos += nDataCount + 4;
                    }
                }
            }

            if (nHandled >= 0)
            {
                Buf.RemoveRange(0, nHandled + nDataCount + 4); //从队列中删除已处理的数据
            }
        }

        private static void DataHandle(List<byte> arr)
        {

            var cfg = Common.TestConfig;
            string str = "";
            // 结果 0 不合格； 1 合格； 2 无效
            const int bad = 0;
            const int good = 1;
            const int invalid = 2;
            const int invalidDecayValue = -1;
            /* 
   AA:头  
arr[1] 命令列表：
            0x01 - 设备参数上传
            0x02 - 试验开始
            0x03 - 试验结束
            0x04 - 实时数据传输
            0x05 - GMP请求                  
        */

            switch (arr[1])
            {
                /* 01H-参数
                    上控温：  2Bytes  单位：℃ 1位小数
                    下控温：  2Bytes  单位：℃  1位小数
                    设定压力：4Bytes 小数位数根据单位判断 "kPa","mbar","psi","mmHg"对应0,0,1,0位小数
                    试验时间：4Bytes 单位：秒 2位小数
                    样品信息：8Bytes 最长8字节，不足8字节用' '填充
                    件数：    2Bytes 16位无符号整数
                    压力单位：1Byte 0x00-kPa 0x01-mbar 0x02-psi 0x03-mmHg
                */
                case 0x01: //参数
                    int nUnit = arr[25];
                    Common.unitPressure = Common.arrUnitP[nUnit];
                    Common.nDigitPressure = arr[25] == 2 ? 1 : 0; //"kPa","mbar","psi","mmHg"对应0,0,1,0位小数

                    var p1 = DcProtocol.DicParam[1010];
                    double prUpperTemp = (int)GetValueByAddress(p1.Address) / Math.Pow(10, p1.DecimalPlace); // 上封头温度
                    var p2 = DcProtocol.DicParam[1020];
                    double prLowerTemp = GetValueByAddress(p2.Address) / Math.Pow(10, p2.DecimalPlace); // 下封头温度
                    var p3 = DcProtocol.DicParam[1030];
                    double prPressure = GetValueByAddress(p3.Address) / Math.Pow(10, Common.nDigitPressure); // 压强
                    var p4 = DcProtocol.DicParam[1040];
                    double prTime = GetValueByAddress(p4.Address) / Math.Pow(10, p4.DecimalPlace); // 时间 
                    var p5 = DcProtocol.DicParam[1060];
                    Common.nSampleCount = (int)GetValueByAddress(p5.Address); //件数
                    byte[] bytes = arr.GetRange(15, 8).ToArray();
                    string sampleInfo = System.Text.Encoding.UTF8.GetString(bytes); //样品信息

                    Log.Info($"收到参数：上封头温度:{prUpperTemp:F1},下封头温度:{prLowerTemp:F1}热封压力:{prPressure:F1},,设定时间:{prTime:F2},件数:{Common.nSampleCount},样品信息:{sampleInfo},压力单位:{Common.unitPressure}");

                    _onReceiveParameterHandler?.Invoke(null,
                        new ComParameterEventArgs { PmTime = prTime, PmPressure = prPressure, PmUpperTemp = prUpperTemp, PmLowerTemp = prLowerTemp, SampleInfo = sampleInfo });
                    LogRepository.AddLog(LanguageManager.GetLanguageValue("OP-ReceivedParam"), "Manage");
                    break;

                /* 02H-试验开始
                    编号：2Bytes 16位无符号整数
                    当前次数：2Bytes 16位无符号整数
                */
                case 0x02:
                    if (Common.TestStatus != TestStatus.Start) //开始帧重发，保证不重复处理
                    {
                        int nTestNo = arr[3] * 256 + arr[4];
                        if (Common.nTestNo != nTestNo && Common.TestStatus == TestStatus.End) //切换编号后未保存则保存报告
                        {
                            CurrentRepository.SaveTest(1);
                        }
                        int nTestId = arr[5] * 256 + arr[6];
                        Common.TestStatus = TestStatus.Start;
                        _onTestStartedHandler?.Invoke(null, new ComStartEventArgs { TestNo = nTestNo, TestId = nTestId });
                        Log.Info($"收到试验开始命令，编号：{nTestNo} 次数：{nTestId}。");
                    }
                    //int nDeltaTime = (int)GetValueByAddress(new List<int> { 5, 6, 7, 8 }); // 时间差
                    //_onTestStatusChangedHandler?.Invoke(null, new ComTestStatusEventArgs { TestId = Common.nTestId, Station = Common.nStation, Status = TestStatus.Testing, deltaTime = nDeltaTime });

                    break;

                /* 03H-结束，含结果
                    编号：2Bytes 16位无符号整数
                    当前次数：2Bytes 16位无符号整数
                    结果：1Byte 0x00-不合格 0x01-合格 0x02-手动停止（不保存结果）
                */
                case 0x03:
                    if (Common.TestStatus != TestStatus.End) //结果帧重发，保证不重复处理
                    {
                        Common.TestStatus = TestStatus.End;
                        var r1 = DcProtocol.DicResult[10034];
                        Common.nTestNo = (int)GetValueByAddress(r1.Address); // 试验编号
                        var r2 = DcProtocol.DicResult[3001];
                        Common.nTestId = (int)GetValueByAddress(r2.Address); // 次数
                        int nResult = arr[7]; // 是否合格

                        if (Common.ComInfo.SaveComData)
                        {
                            Log.Info($"收到结果：试验编号:{Common.nTestNo},工位:{Common.nTestId},结果:{nResult}");
                        }
                        _onReceiveResultHandler?.Invoke(null, new ComResultEventArgs { TestNo = Common.nTestNo, TestId = Common.nTestId, nQualified = nResult });
                    }
                    break;

                /* 04H-实时数据
                    上控温：  2Bytes  单位：℃ 1位小数
                    下控温：  2Bytes  单位：℃  1位小数
                    压力：    4Bytes 小数位数根据单位判断 "kPa","mbar","psi","mmHg"对应0,0,1,0位小数
                    时间：    4Bytes 单位：秒 2位小数
                    压力单位：1Byte 0x00-kPa 0x01-mbar 0x02-psi 0x03-mmHg  *注意：此处单位是方便实时数据传输，实际上单位已经由设备参数上传
                */
                case 0x04:
                    var d1 = DcProtocol.DicRealTimeData[2010];
                    double rtUpperTemp = GetValueByAddress(d1.Address) / Math.Pow(10, d1.DecimalPlace); // 上封头温度
                    var d2 = DcProtocol.DicRealTimeData[2020];
                    double rtLowerTemp = GetValueByAddress(d2.Address) / Math.Pow(10, d2.DecimalPlace); // 下封头温度
                    var d3 = DcProtocol.DicRealTimeData[2030];
                    double rtPressure = GetValueByAddress(d3.Address) / Math.Pow(10, Common.nDigitPressure); // 压强 
                    var d4 = DcProtocol.DicRealTimeData[2040];
                    double rtTime = (int)GetValueByAddress(d4.Address) / Math.Pow(10, d4.DecimalPlace); // 时间

                    _onReceiveRealtimeDataHandler?.Invoke(null, new ComRealTimeDataEventArgs { RtTime = rtTime, RtPressure = rtPressure, RtUpperTemp = rtUpperTemp, RtLowerTemp = rtLowerTemp });
                    break;

                /* 设备权限
                0x05 - GMP请求：
                格式：
                请求类型：2Bytes
                    进入试验界面：0x1000
                    进入预设界面：0x1002
                    进入报告界面：0x1005
                    进入用户界面：0x1008
                    进入语言界面：0x1009
                    进入日历界面：    （之前的设备未启用，暂不启用）
                    进入标定界面：0x100b
                    进入系统界面：0x100a
                    打印报告：0x1007
                    删除报告：0x1006
                    修改自动打印：0x1003
                    切换GMP：0x100c
                */
                case 0x05:
                    byte au = 1;
                    switch (arr[4]) //权限占2位，但首字节相同，仅需判断第2字节
                    {
                        case 0x0:
                            if (Common.Permissions.All(p => p.Code != "P_EnterTest".Clear()))
                            {
                                au = 0;
                                Log.Debug("进入试验 无权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_EnterTest") + LanguageManager.GetLanguageValue("NoAuthority"), "TEST");
                            }
                            else
                            {
                                Log.Debug("进入试验 有权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_EnterTest") + LanguageManager.GetLanguageValue("HasAuthority"), "TEST");
                            }
                            break;
                        case 0x02:
                            if (Common.Permissions.All(p => p.Code != "P_EnterSetup".Clear()))
                            {
                                au = 0;
                                Log.Debug("进入设置 无权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_EnterSetup") + LanguageManager.GetLanguageValue("NoAuthority"), "TEST");
                            }
                            else
                            {
                                Log.Debug("进入设置 有权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_EnterSetup") + LanguageManager.GetLanguageValue("HasAuthority"), "TEST");
                            }
                            break;
                        case 0x05:
                            if (Common.Permissions.All(p => p.Code != "P_EnterReport".Clear()))
                            {
                                au = 0;
                                Log.Debug("进入报告 无权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_EnterReport") + LanguageManager.GetLanguageValue("NoAuthority"), "TEST");
                            }
                            else
                            {
                                Log.Debug("进入报告 有权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_EnterReport") + LanguageManager.GetLanguageValue("HasAuthority"), "TEST");
                            }
                            break;
                        case 0x08:
                            if (Common.Permissions.All(p => p.Code != "P_EnterUser".Clear()))
                            {
                                au = 0;
                                Log.Debug("进入用户设置 无权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_EnterUser") + LanguageManager.GetLanguageValue("NoAuthority"), "TEST");
                            }
                            else
                            {
                                Log.Debug("进入用户设置 有权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_EnterUser") + LanguageManager.GetLanguageValue("HasAuthority"), "TEST");
                            }
                            break;
                        case 0x09:
                            if (Common.Permissions.All(p => p.Code != "P_EnterLan".Clear()))
                            {
                                au = 0;
                                Log.Debug("进入语言设置 无权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_EnterLan") + LanguageManager.GetLanguageValue("NoAuthority"), "TEST");
                            }
                            else
                            {
                                Log.Debug("进入语言设置 有权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_EnterLan") + LanguageManager.GetLanguageValue("HasAuthority"), "TEST");
                            }
                            break;
                        case 0x0B:
                            if (Common.Permissions.All(p => p.Code != "P_Cali".Clear()))
                            {
                                au = 0;
                                Log.Debug("进入标定 无权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_Cali") + LanguageManager.GetLanguageValue("NoAuthority"), "TEST");
                            }
                            else
                            {
                                Log.Debug("进入标定 有权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_Cali") + LanguageManager.GetLanguageValue("HasAuthority"), "TEST");
                            }
                            break;
                        case 0x0A:
                            if (Common.Permissions.All(p => p.Code != "P_EnterSystem".Clear()))
                            {
                                au = 0;
                                Log.Debug("进入系统设置 无权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_EnterSystem") + LanguageManager.GetLanguageValue("NoAuthority"), "TEST");
                            }
                            else
                            {
                                Log.Debug("进入系统设置 有权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_EnterSystem") + LanguageManager.GetLanguageValue("HasAuthority"), "TEST");
                            }
                            break;
                        case 0x07:
                            if (Common.Permissions.All(p => p.Code != "P_PrintReport".Clear()))
                            {
                                au = 0;
                                Log.Debug("打印报告 无权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_PrintReport") + LanguageManager.GetLanguageValue("NoAuthority"), "TEST");
                            }
                            else
                            {
                                Log.Debug("打印报告 有权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_PrintReport") + LanguageManager.GetLanguageValue("HasAuthority"), "TEST");
                            }
                            break;
                        case 0x06:
                            if (Common.Permissions.All(p => p.Code != "P_DeleteReport".Clear()))
                            {
                                au = 0;
                                Log.Debug("删除报告 无权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_DeleteReport") + LanguageManager.GetLanguageValue("NoAuthority"), "TEST");
                            }
                            else
                            {
                                Log.Debug("删除报告 有权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_DeleteReport") + LanguageManager.GetLanguageValue("HasAuthority"), "TEST");
                            }
                            break;
                        case 0x03:
                            if (Common.Permissions.All(p => p.Code != "P_ModifyAutoPrint".Clear()))
                            {
                                au = 0;
                                Log.Debug("修改自动打印 无权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_ModifyAutoPrint") + LanguageManager.GetLanguageValue("NoAuthority"), "TEST");
                            }
                            else
                            {
                                Log.Debug("修改自动打印 有权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_ModifyAutoPrint") + LanguageManager.GetLanguageValue("HasAuthority"), "TEST");
                            }
                            break;
                        case 0x0C:
                            if (Common.Permissions.All(p => p.Code != "P_GmpSwitch".Clear()))
                            {
                                au = 0;
                                Log.Debug("切换GMP 无权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_GmpSwitch") + LanguageManager.GetLanguageValue("NoAuthority"), "TEST");
                            }
                            else
                            {
                                Log.Debug("切换GMP 有权限");
                                LogRepository.AddLog(LanguageManager.GetLanguageValue("P_GmpSwitch") + LanguageManager.GetLanguageValue("HasAuthority"), "TEST");
                            }
                            break;
                        default:
                            break;
                    }
                    SendCommand(arr[3], arr[4], au);
                    //if (Common.Permissions.All(p => p.Code != "P_Device".Clear()))
                    //{
                    //    Log.Debug("标定 无权限");
                    //    SendCommand(arr[3], arr[4], 0);
                    //    LogRepository.AddLog(LanguageManager.GetLanguageValue("P_EnterTest") + LanguageManager.GetLanguageValue("NoAuthority"), "TEST");
                    //}
                    //else
                    //{
                    //    Log.Debug("标定 有权限");
                    //    SendCommand(arr[3], arr[4], 1);
                    //    LogRepository.AddLog(LanguageManager.GetLanguageValue("P_EnterTest") + LanguageManager.GetLanguageValue("HasAuthority"), "TEST");
                    //}
                    break;

                default:
                    break;
            }

            double GetValueByAddress(IEnumerable<int> address)
            {
                IEnumerable<int> address1 = address;
                int[] x = address1.ToArray();
                List<int> rd = x.Select(t => t + 2).ToList();

                List<byte> bytes = rd.Select(ad => arr[ad]).ToList();
                if (x.Length == 4)
                {
                    return GetInt32Value(bytes.ToArray());
                }

                if (x.Length == 2)
                {
                    return GetInt16Value(bytes.ToArray());
                }

                if (x.Length == 1)
                {
                    return bytes[0];
                }

                return 0;
            }

            int GetInt32Value(params byte[] values)
            {
                int r = 0;
                var vs = values.Reverse().ToArray();
                r = BitConverter.ToInt32(vs, 0);

                return r;
            }

            short GetInt16Value(params byte[] values)
            {
                short r = 0;
                byte[] vs = values.Reverse().ToArray();
                r = BitConverter.ToInt16(vs, 0);

                return r;
            }
        }


        public static void OnReceivedByteCountChanged(int received)
        {
            _onReceivedByteCountChangedHandler?.Invoke(null, new ComByteCountEventArgs { ReceivedCount = received, SentCount = -1 });
        }

        public static void OnSentByteCountChanged(int sent)
        {
            _onSentByteCountChangedHandler?.Invoke(null, new ComByteCountEventArgs { ReceivedCount = -1, SentCount = sent });
        }
    }
}
